package aar.test.tessocr;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.google.gson.Gson;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import aar.test.tessocr.base.BaseActivity;
import aar.test.tessocr.baseApplication.BaseApplication;
import aar.test.tessocr.model.DialogCallback;
import aar.test.tessocr.model.LzyResponse;
import aar.test.tessocr.model.UnexporderListModel;
import aar.test.tessocr.utils.ConstantPara;
import aar.test.tessocr.utils.PrefUtils;
import aar.test.tessocr.utils.Urls;
import butterknife.ButterKnife;
import butterknife.OnClick;
import clx.tessocr.ocreng.R;
import lib_zxing.activity.CodeUtils;
import okgo.OkGo;
import okgo.model.Response;
import okhttp3.RequestBody;

import static aar.test.tessocr.Login1Activity.JSON;
import static aar.test.tessocr.MainActivity.APP_KEY;

/**
 * Created by sunshine on 2020/7/23
 */
public class DepositOrderMainActivity extends BaseActivity {
    private List<UnexporderListModel> mList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_deposit_order_main);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (null != data) {
                int mm = data.getIntExtra(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_FAILED);
                if (mm == CodeUtils.RESULT_SUCCESS) {
                    getData(BaseApplication.getIntance().getLoginCabModel().getCabinetid());
                }
            }
        }
    }

    @OnClick({R.id.left_layout, R.id.deposit_order_button, R.id.deposit_finish_button})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.left_layout:
                finish();
                break;
            case R.id.deposit_order_button:
                /**
                 * 在PreviewActivity中自定义相机拍摄模块，然后直接调用SDK中的身份证预览方法进行识别
                 * PreviewActivity:使用线程定时对焦方案 PreviewActivityAuto:使用的是camera自动对焦参数设置连续对焦
                 * 客户需要集成自定义的PreviewActivity需要拷贝到集成项目中
                 */
                /*if (!PrefUtils.getBoolean(this, "cabinetLoginFlag", false)) {
                    Toast.makeText(DepositOrderMainActivity.this,"没有柜子信息，请扫描登录",Toast.LENGTH_SHORT).show();
                    return;
                }*/

                Intent intent = new Intent(this, AppointmentOrderNewActivity.class);
                // 合合信息授权提供的APP_KEY
                intent.putExtra(MainActivity.EXTRA_KEY_APP_KEY, APP_KEY);
                startActivity(intent);
                break;
            case R.id.deposit_finish_button:
                //扫码完成投递，需先判断是否已经登录柜子，如果没有登录则登录，之后判断此柜子是否有逾期件要先处理再进行扫码投递
                //如果登录了柜子，则直接判断此柜子是否有逾期件要先处理再进行扫码投递
                if (!PrefUtils.getBoolean(this, "cabinetLoginFlag", false)) {
                //Toast.makeText(DepositOrderMainActivity.this,"没有柜子信息，请扫描登录",Toast.LENGTH_SHORT).show();
                intent = new Intent(this, ScanLoginCabActivity.class);
                intent.putExtra("from", "isFromAppointment");
                startActivityForResult(intent, 100);
            } else {
              getData(PrefUtils.getString(DepositOrderMainActivity.this, "cabinetId", ""));
            }

                break;
        }
    }

    private void getData(String cabinetId) {
        if (TextUtils.isEmpty(PrefUtils.getString(this, "userId", ""))) {
            Toast.makeText(DepositOrderMainActivity.this, "登录信息为空", Toast.LENGTH_SHORT).show();
            return;

        }

        final Map<String, Object> map = new HashMap<>();
        map.put("adminid", PrefUtils.getString(this, "userId", ""));
        map.put("keyword", "");
        map.put("page", "1");
        map.put("size", ConstantPara.PAGESIZE_MAX);
        map.put("cabinetId", cabinetId);


        Gson gg = new Gson();
        String json = gg.toJson(map);

        RequestBody body = RequestBody.create(JSON, json);
        OkGo.<LzyResponse<List<UnexporderListModel>>>post(Urls.URL_TIMEOUT_LIST)//
                .tag(this)//
                .upRequestBody(body)
                .isMultipart(false)         //强制使用 multipart/form-data 表单上传（只是演示，不需要的话不要设置。默认就是false）
                .execute(new DialogCallback<LzyResponse<List<UnexporderListModel>>>(DepositOrderMainActivity.this) {
                    @Override
                    public void onSuccess(Response<LzyResponse<List<UnexporderListModel>>> response) {
                        mList.clear();
                        mList.addAll(response.body().data);
                        if (mList.size() == 0) {
                            Intent intent1 = new Intent(DepositOrderMainActivity.this, OpenCabActivity.class);
                            // 合合信息授权提供的APP_KEY
                            intent1.putExtra(MainActivity.EXTRA_KEY_APP_KEY, APP_KEY);
                            startActivity(intent1);
                        } else {
                            Toast.makeText(DepositOrderMainActivity.this,"请先处理当前柜子的逾期件!",Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent();
                            Bundle bundle = new Bundle();
                            bundle.putSerializable("unexporderListModel", (Serializable) mList);
                            intent.putExtras(bundle);
                            intent.setClass(DepositOrderMainActivity.this,TimeOutListForCabinetActivity.class);
                            startActivity(intent);
                        }
                    }

                    @Override
                    public void onError(Response<LzyResponse<List<UnexporderListModel>>> response) {
                        Intent intent1 = new Intent(DepositOrderMainActivity.this, OpenCabActivity.class);
                        // 合合信息授权提供的APP_KEY
                        intent1.putExtra(MainActivity.EXTRA_KEY_APP_KEY, APP_KEY);
                        startActivity(intent1);
                    }
                });
    }
}
